
package org.chart4jsf;

import java.io.IOException;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import javax.faces.render.Renderer;

@FacesRenderer(componentFamily = "javax.faces.Input", rendererType = "HtmlHelloWorld")
public class HtmlHelloWorldRenderer extends Renderer {

    /* (non-Javadoc)
     * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
     */
    @Override
    public void decode(FacesContext context, UIComponent component) {

	Map requestMap = context.getExternalContext().getRequestParameterMap();
	String clientId = component.getClientId(context);
	char sep = UINamingContainer.getSeparatorChar(context);
	String submitted_hello_msg = ((String) requestMap.get(clientId + sep + "inputfield"));
	((UIInput) component).setSubmittedValue(submitted_hello_msg);
    }

    /* (non-Javadoc)
     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
     */
    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {

	String clientId = component.getClientId(context);
	char sep = UINamingContainer.getSeparatorChar(context);
	this.encodeInputField(context, clientId + sep + "inputfield", (UIInput) component);
	this.encodeSubmitButton(context, clientId + sep + "submit", (UIInput) component);
	this.encodeOutputField(context, (UIInput) component);
    }

    private void encodeInputField(FacesContext context, String clientId, UIInput component) throws IOException {

	// Render a standard HTML input field
	ResponseWriter writer = context.getResponseWriter();
	writer.startElement("input", component);
	writer.writeAttribute("type", "text", null);
	writer.writeAttribute("name", clientId, "clientId");
	Object value = component.getValue();
	if (value != null) {
	    writer.writeAttribute("value", value.toString(), "value");
	}
	writer.writeAttribute("size", "6", null);
	writer.endElement("input");
    }

    private void encodeSubmitButton(FacesContext context, String clientId, UIInput component) throws IOException {

	// render a submit button
	ResponseWriter writer = context.getResponseWriter();
	writer.startElement("input", component);
	writer.writeAttribute("type", "Submit", null);
	writer.writeAttribute("name", clientId, "clientId");
	writer.writeAttribute("value", "Click Me!", null);
	writer.endElement("input");
    }

    private void encodeOutputField(FacesContext context, UIInput component) throws IOException {

	ResponseWriter writer = context.getResponseWriter();
	String hellomsg = (String) component.getAttributes().get("value");
	writer.startElement("p", component);
	writer.writeText("You entered: " + hellomsg, null);
	writer.endElement("p");
    }

}
